package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author Wq
 * @since 2022-10-05
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    @Override
    public ResponseResult<ApAuthor> saveAuthor(ApAuthor author) {
        //1、参数校验
        if (author == null) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断是否已经是文章作者
        // 根据user_id查询
        LambdaQueryWrapper<ApAuthor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApAuthor::getUserId, author.getUserId());
        ApAuthor authorDB = this.getOne(wrapper);
        if (authorDB == null) {
            author.setCreatedTime(new Date());
            this.save(author);
            return ResponseResult.okResult(author);
        } else {
            return ResponseResult.okResult(authorDB);
        }
    }
}
